@{
ViewData["Title"] = "商品管理";
var commoditys = ViewBag.Data.Commoditys;
var pagebar = ViewBag.Data.PageBar;
}

<!-- start page title -->
<div class="row">
  <div class="col-12">
    <div class="page-title-box d-sm-flex align-items-center justify-content-between">
      <h4 class="mb-sm-0">商品管理</h4>

      <div class="page-title-right">
        <ol class="breadcrumb m-0">
          <li class="breadcrumb-item">
            <a asp-controller="Home"
               asp-action="Index">首页
            </a>
          </li>
          <li class="breadcrumb-item active">商品管理</li>
        </ol>
      </div>

    </div>
  </div>
</div>
<!-- end page title -->

<div class="card mt-2 mb-2"
     style="min-height: 400px;">
  <div class="card-header d-flex justify-content-between">
    <span>商品列表</span>
    <button type="button"
            class="btn btn-primary"
            id="btn-addnew">新增</button>
  </div>
  <div class="card-body">
    <table class="table table-bordered">
      <thead class="thead-light">
        <tr>
          <th>商品名</th>
          <th>单位</th>
          <th>价钱</th>
          <th style="width: 200px;">操作</th>
        </tr>
      </thead>
      <tbody>
        @foreach(var p in commoditys){
        <tr>
          <td>@p.Cm_name</td>
          <td>@p.firm</td>
          <td>@p.Price</td>
          <td>
            <button type="button"
                    class="btn btn-warning btn-update"
                    data-commodityid="@p.CommodityID">修改</button>
            <button type="button"
                    class="btn btn-danger btn-del"
                    data-cname="@p.Cm_name"
                    data-commodityid="@p.CommodityID">删除</button>
          </td>
        </tr>
        }
      </tbody>
    </table>
    <!-- <blockquote class="blockquote mb-0">
      <p>A well-known quote, contained in a blockquote element.</p>
      <footer class="blockquote-footer">Someone famous in <cite title="Source Title">Source Title</cite></footer>
    </blockquote> -->
  </div>
  <div class="card-footer d-flex justify-content-end"
       style="height: 60px;">
    <nav aria-label="..."
         class="mt-1">
      <ul class="pagination">
        <li class='page-item @(pagebar.CurrentPage==1?"disabled":"")'>
          <a class="page-link"
             asp-area="admin"
             asp-controller="Commodity"
             asp-action="GetPage"
             asp-route-page="@pagebar.Previous"
             asp-route-pageSize="@pagebar.PageSize">Previous</a>
        </li>
        @foreach(var btn in pagebar.ButtonGroup){
        <li class='page-item @(pagebar.CurrentPage==btn?"active":"")'>
          <a class="page-link"
             asp-area="admin"
             asp-controller="Commodity"
             asp-action="GetPage"
             asp-route-page="@btn"
             asp-route-pageSize="@pagebar.PageSize">@btn</a>
        </li>
        }
        <li class='page-item @(pagebar.CurrentPage==pagebar.PageCount?"disabled":"")'>
          <a class="page-link"
             asp-area="admin"
             asp-controller="Commodity"
             asp-action="GetPage"
             asp-route-page="@pagebar.Next"
             asp-route-pageSize="@pagebar.PageSize">Next</a>
        </li>
      </ul>
    </nav>
  </div>
</div>


@section DemoScripts{
<script>
  $(function () {
    //激活菜单项
    $("#menu_commoditys").addClass("active");
    //删除处理
    $(".btn-del").on("click", function () {
      let btn = $(this);
      let msg = `是否删除【${btn.data("cname")}】，请确认`;
      if (!confirm(msg)) return;
      $.ajax({
        type: "delete",
        url: `/admin/commoditys/info/${btn.data("commodityid")}`,
        success: function (response) {
          alert(response.message);
          if (response.status == 0) location.reload();
        }
      });
    });

    //修改处理
    $(".btn-update").on("click", function () {
      location.href = `/admin/commoditys/info/${$(this).data("commodityid")}`;
    });
    //新增处理
    $("#btn-addnew").on("click", function () {
      location.href = "/admin/commoditys/info/0";
    });
  });
</script>
}
